package org.itboys.mobile.controller.web;

import org.itboys.commons.CommonConstants;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mobile.entity.mongo.SundayLouKa;
import org.itboys.mobile.service.mongo.web.SundayLouKaService;
import org.itboys.mobile.util.ResponseMessage;
import org.itboys.param.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Auther: 罗尧坤
 * Date: 2018/7/17
 * Description: 集楼_楼卡接口_web_控制器
 */

@Controller
@RequestMapping("/sunday/web/louka")
public class SundayLouKaController extends BaseController {

    @Autowired
    private SundayLouKaService loukaService;

    /**
     * 楼卡_web_主页
     * @return
     */
    @RequestMapping("/index")
    public String index(HttpServletRequest request,
                        HttpServletResponse response
    ){
        return "/sunday/louka/index";
    }

    /**
     * 获取楼卡信息_web
     * @param request
     * @param response
     */
    @RequestMapping("/select")
    public void select(HttpServletRequest request,
                       HttpServletResponse response
    ){
        try {
            PageResult<SundayLouKa> result = loukaService.select(request);
            JsonPageUtils.renderJsonPage(result.getTotal(),result.getData(),response);
        } catch (Exception e) {
            String msg ="";
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
            }else{
                msg= CommonConstants.ERROMSG;
                logger.error("/sunday/web/louka/select------"+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(CommonConstants.FAIL_CODE, CommonConstants.FAIL);
        }
    }

    /**
     * 跳转到具体修改或增加页面
     * @param request
     * @param response
     * @param id 楼卡id
     * @param model
     * @return
     */
    @RequestMapping("/input")
    public String input(HttpServletRequest request,
                        HttpServletResponse response,
                        @RequestParam(value="id",required=true) Long id,
                        Model model
    ){
        SundayLouKa louka = loukaService.findOne(id);
        model.addAttribute("louka", louka);
        return "/sunday/louka/input";
    }

    /**
     * 保存楼卡
     * @param request
     * @param response
     * @param louka
     * @param imageFile
     */
    @RequestMapping("/save")
    public void save(HttpServletRequest request,
                     HttpServletResponse response,
                     @ModelAttribute SundayLouKa louka,
                     @RequestParam(value="imageFile",required=false) MultipartFile imageFile,
                     @RequestParam(value="defaultImageFile",required=false) MultipartFile defaultImageFile
    ){
        try {
            loukaService.saveBuilding(louka, imageFile, defaultImageFile);
            ResponseMessage.success();
        } catch (Exception e) {
            e.printStackTrace();
            String msg = " ";
            if (e.getClass().getSimpleName().equalsIgnoreCase("Error Exception")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/sunday/web/louka/save------" + e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(CommonConstants.FAIL_CODE, CommonConstants.FAIL);

        }

    }

    /**
     * 删除楼卡
     * @param request
     * @param response
     * @param id 楼卡id
     */
    @RequestMapping("/delete")
    public void delete(HttpServletRequest request,
                       HttpServletResponse response,
                       @RequestParam(value="id",required=true) Long id
    ){
        try {
            loukaService.delete(id);
            ResponseMessage.success();
        } catch (Exception e) {
            e.printStackTrace();
            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("Error Exception")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/sunday/web/louka/delete------" + e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(CommonConstants.FAIL_CODE, CommonConstants.FAIL);
        }
    }

    /**
     * 楼卡选择器
     * @param request
     * @param isSingle
     * @param callBack
     * @param param
     * @param chooseId
     * @param model
     * @return
     */
    @RequestMapping("/choose" )
    public  String choose(HttpServletRequest request,
                          @RequestParam(value="isSingle",required=true)Integer isSingle,//是否单选
                          @RequestParam(value="callBack",required=false)String callBack,//回调参数
                          @RequestParam(value="param",required=false)String param,//查询参数
                          @RequestParam(value="chooseId",required = false)String chooseId,//选择器id

                          Model model){
        model.addAttribute("param",param);
        model.addAttribute("callBack",callBack);
        model.addAttribute("chooseId",chooseId);
        if(isSingle==null){
            return "/sunday/louka/choose/choose_erro";
        }else if(isSingle==1){
            return "/sunday/louka/choose/choose_single";
        }

        return "/finance/louka/choose/choose_erro";
    }
    
}
